package com.kaikeba.controller;

import com.kaikeba.bean.BootStrapTableUserExpress;
import com.kaikeba.bean.Message;
import com.kaikeba.bean.ResultData;
import com.kaikeba.bean.UserExpress;
import com.kaikeba.mvc.ResponseBody;
import com.kaikeba.service.UserExpressService;
import com.kaikeba.util.DateFormatUtil;
import com.kaikeba.util.JSONUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class UserExpressController {

    @ResponseBody("/user/console.do")
    public String console(HttpServletRequest request, HttpServletResponse response){
        Map<String,Integer> data = UserExpressService.console();
        Message msg = new Message();
        if(data.size() == 0){
            //查询失败
            msg.setStatus(-1);
        }else {
            //查询成功
            msg.setStatus(0);
        }
        msg.setData(data);
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/user/insert.do")
    public String insert(HttpServletRequest request,HttpServletResponse response){
        //获取前天传过来的值
        String username = request.getParameter("username");
        String userPhone = request.getParameter("userPhone");
        String id_card = request.getParameter("id_card");
        String password = request.getParameter("password");
        //判断手机号码与身份证号是否重复
        UserExpress byPhone = UserExpressService.findByPhone(userPhone);
        UserExpress byIdCard = UserExpressService.findByIdCard(id_card);
        Message msg = new Message();
        //如果查询出这两个都为null   就表明没用重复
        if(byIdCard == null && byPhone == null) {
            UserExpress e = new UserExpress(username, userPhone, id_card, password);
            boolean insert = UserExpressService.insert(e);
            if (insert) {
                msg.setResult("注册成功！");
                msg.setStatus(0);
            }else {
                msg.setResult("抱歉,注册失败~");
                msg.setStatus(-1);
            }
        }else {
            msg.setResult("抱歉,注册失败~,手机号或身份证号已被注册");
            msg.setStatus(-1);
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }


    @ResponseBody("/user/list.do")
    public String list(HttpServletRequest request,HttpServletResponse response){
        //获取前端传来的数据
        int offset = Integer.parseInt(request.getParameter("offset"));
        int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
        List<UserExpress> all = UserExpressService.findAll(true, offset, pageNumber);
        List<BootStrapTableUserExpress> list2 = new ArrayList<>();
        for (UserExpress es : all) {
            String createTime = DateFormatUtil.format(es.getCreateTime());
            String loginTime = es.getLoginTime()==null?"-":DateFormatUtil.format(es.getLoginTime());
            BootStrapTableUserExpress e = new BootStrapTableUserExpress(es.getId(),es.getUsername(),es.getUserPhone(),es.getPassword(),createTime,loginTime);
            list2.add(e);
        }
        //获取用户总数
        Integer total = UserExpressService.console().get("data_size");
        ResultData<BootStrapTableUserExpress> data = new ResultData<>();
        data.setTotal(total);
        data.setRows(list2);
        String json = JSONUtil.toJSON(data);
        return json;
    }

    @ResponseBody("/user/find.do")
    public String findByPhone(HttpServletRequest request,HttpServletResponse response){
        //获取前台传来的参数
        String userPhone = request.getParameter("userPhone");
        UserExpress data = UserExpressService.findByPhone(userPhone);
        Message msg = new Message();
        if(data != null){
            msg.setStatus(0);
            msg.setResult("查询成功");
        }else {
            msg.setStatus(-1);
            msg.setResult("查询失败");
        }
        msg.setData(data);
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/user/update.do")
    public String update(HttpServletRequest request,HttpServletResponse response){
        //获取前端传递过来的信息
        int id = Integer.parseInt(request.getParameter("id"));
        String username = request.getParameter("username");
        String userPhone = request.getParameter("userPhone");
        String idCard = request.getParameter("IDCard");
        String password = request.getParameter("password");
        //判断手机号码与身份证号是否重复
        UserExpress byPhone = UserExpressService.findByPhone(userPhone);
        UserExpress byIdCard = UserExpressService.findByIdCard(idCard);
        Message msg = new Message();
        if((byPhone != null && byPhone.getId() != id) || (byIdCard != null && byIdCard.getId() != id)){
            //证明有重复的
            msg.setStatus(-1);
            msg.setResult("修改失败,手机号或身份证号重复");
        }else {
            UserExpress e = new UserExpress(username, userPhone, idCard, password);
            //调用service方法
            boolean flag = UserExpressService.update(id, e);
            if (flag) {
                //修改成功
                msg.setStatus(0);
                msg.setResult("修改成功");
            } else {
                msg.setStatus(-1);
                msg.setResult("修改失败");
            }
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/user/delete.do")
    public String delete(HttpServletRequest request,HttpServletResponse response){
        //获取前台传来的参数
        int id = Integer.parseInt(request.getParameter("id"));
        boolean flag = UserExpressService.delete(id);
        Message msg = new Message();
        if(flag){
            //删除成功
            msg.setStatus(0);
            msg.setResult("删除成功");
        }else{
            msg.setStatus(-1);
            msg.setResult("删除失败");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }
}
